<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath()}+'/static/'">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="Mosaddek">
    <meta name="keyword" content="FlatLab, Dashboard, Bootstrap, Admin, Template, Theme, Responsive, Fluid, Retina">
    <link rel="shortcut icon" href="img/favicon.html">

    <title>Basic Table</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-reset.css" rel="stylesheet">
    <!--external css-->
    <link href="assets/font-awesome/css/font-awesome.css" rel="stylesheet"/>
    <!-- Custom styles for this template -->
    <link href="css/style.css" rel="stylesheet">
    <link href="css/style-responsive.css" rel="stylesheet"/>

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 tooltipss and media queries -->
    <!--[if lt IE 9]>
    <script src="js/html5shiv.js"></script>
    <script src="js/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="plugins/ztree/css/bootstrapStyle/bootstrapStyle.css" type="text/css">

</head>

<body>
<section class="wrapper">
    <div class="panel panel-primary">
        <div class="panel panel-heading">
            给角色【<span th:text="${sysRole.roleName}"></span>】赋权
        </div>
        <div class="panel-body">
<!--       创建隐藏文本 把角色id放进去 用于做赋权操作     -->
            <input type="hidden" id="id" th:value="${sysRole.roleCode}" />
<!--    树形结构的俩行代码        -->
    <ul id="tree" class="ztree" style="width:260px; overflow:auto;"></ul>
    <input type="hidden" th:value="${moduleList}" id="menuJson"/>
    <button type="button" onclick="save()" class="btn btn-warning btn-sm">保存权限</button>
        </div>
    </div>
</section>
</body>


<!-- js placed at the end of the document so the pages load faster -->
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.scrollTo.min.js"></script>
<script src="js/jquery.nicescroll.js" type="text/javascript"></script>
<!--common script for all pages-->
<script src="js/common-scripts.js"></script>
<script type="text/javascript" src="plugins/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="plugins/ztree/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="plugins/ztree/js/jquery.ztree.exedit.js"></script>
<SCRIPT type="text/javascript" >

    var zTree;
    var demoIframe;

    var setting = {
        check:{
            enable: true
        },
        data: {
            simpleData: {
                enable:true,
                idKey: "moduleCode",
                pIdKey: "parentModule",
                rootPId: "01"
            },
            key:{
                name:"moduleName"
            }
        },

    };
    //初始化树
    $(document).ready(function(){
        var t = $("#tree");
        //将zNodes数据替换成后端查询的数据 注意：数据以json对象数据存入
        t = $.fn.zTree.init(t, setting, JSON.parse($("#menuJson").val()));
    });
    function save() {
        //1.获取所有选择菜单的idKey
        var arry = $.fn.zTree.getZTreeObj("tree").getCheckedNodes(true);
        var moduleArry = [];
        for (let i = 0; i < arry.length ; i++) {
           moduleArry.push(arry[i].moduleCode);
        }
        console.log(moduleArry)
        //2.获取当前操作的角色id
        var roleId =$("#id").val();
        $(function () {
            console.log(moduleArry);
            //3.通过异步请求将数据发送到后台
            $.ajax({
                url: "/role/savePerms",
                type:'post',
                traditional:true,
                data:{
                    'roleId':roleId,
                    'moduleArry':moduleArry
                },
                success:function (data) {
                    //4.保存成功后返回一个boolean值 判断是否要刷新页面
                    console.log(data);
                    if(data){
                        window.location.reload()
                    }else{
                        alert(data.message);
                    }
                },


            })

        })

    }

    //-->
</SCRIPT>
</html>